<template>
  <div>
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-dark">
          <h2>采购入库</h2>
        </div>
      </el-col>
    </el-row>

    <el-dialog
        :visible.sync="addProduct1"
        width="60%"
        @opened="open"
    >
      <!--          <FindByProductId :movie-id="FindByProductIdId" v-if="FindByProductId2" ref="FindByProductIdRef"></FindByProductId>-->
      <AddProduct :movie-id="addProductId" v-if="addProduct2" ref="addProductRef"></AddProduct>
    </el-dialog>


    <el-row>
      <el-col :span="12">
        <span>供应商:</span>
        <el-select @change="searchSelect(supplierValue)" v-model="supplierValue" placeholder="请选择"
                   style="margin-left: 10px">
          <el-option
              v-for="item in supplierOptions"
              :key="item.supplierId"
              :label="item.supplierName"
              :value="item.supplierId">
          </el-option>
        </el-select>

      </el-col>
      <el-col :span="12">
        <span>采购人:{{ userName }}</span>
      </el-col>
    </el-row>

    <el-row style="margin-top: 100px">
      <el-col :span="24">
        <div class="grid-content bg-purple-dark">
          <el-table
              :data="tableData"
              highlight-current-row
              @current-change="handleCurrentChange"
              style="width: 100%">
            <el-table-column
                prop="productName"
                label="商品名称"
                align="center">
            </el-table-column>
            <el-table-column
                prop="productFirm"
                label="厂家"
                align="center">
            </el-table-column>
            <el-table-column
                prop="productBuyer"
                label="建议采购价"
                align="center">
            </el-table-column>
            <el-table-column
                prop="productSell"
                label="建议销售价"
                align="center">
            </el-table-column>
            <el-table-column
                prop="number"
                label="采购数量"
                align="center">
              <template slot-scope="scope">
                <el-input-number v-model="scope.row.num"
                                 size="small"
                                 @change="handleChange(scope.row,scope.row.num)"
                                 :min="0"
                                 :max="99999"
                                 :controls="false">
                </el-input-number>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>

    <el-row style="margin-top: 100px">
      <el-col :span="8">
        <div class="grid-content bg-purple">
          <el-button type="primary" plain @click="addProduct">添加商品</el-button>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content bg-purple-light">
          <el-button type="primary" plain @click="delProduct">移除商品</el-button>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content bg-purple">
          <el-button type="primary" plain @click="addStock">入库</el-button>
        </div>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import AddProduct from "./addProduct";


export default {
  name: "procurementAndStorage",
  components: {AddProduct},
  data() {
    return {
      addProduct1: false,
      addProductId: 0,
      addProduct2: false,

      supplierId: '',
      userId: '',

      tableData: [],
      currentRow: '',
      total: 0,
      pageSize: 100,
      currentPage: 1,

      clientName: '',
      startDate: '',
      endDate: '',

      supplierValue: '',
      supplierOptions: [],

      userName: '',

      productIdArray: [],
      number: []
    }
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      let params = {
        current: this.currentPage,
        size: this.pageSize,
        supplierName: null,
        supplierScope: null
      }
      this.$axios.get('drp/supplier/findByItem', {params: params}).then(
          r => {
            this.supplierOptions = r.data.records;
            this.total = r.data.total;
          }
      )
      let loginUser = sessionStorage.getItem("loginUser")
      if (sessionStorage.getItem("loginUser") != null) {
        this.userName = JSON.parse(loginUser).realName
        this.userId = JSON.parse(loginUser).userId
      }
    },
    searchSelect(value) {
      this.supplierId = value
      let params = {
        supplierId: value
      }
      this.$axios.get('drp/supplier/findById', {params: params}).then(
          r => {
            console.log(r.data.productBeanList)
            this.tableData = r.data.productBeanList
          }
      )
    },
    handleChange(value, num) {
      if (this.productIdArray.length > 0) {
        for (let i = 0; i < this.productIdArray.length; i++) {
          if (this.productIdArray[i] === value.productId) {
            this.productIdArray.splice(i, 1)
            this.number.splice(i, 1)
          }
        }
      }
      if (num === 0) {
        return
      }
      this.productIdArray.push(value.productId)
      this.number.push(num)
    },
    handleCurrentChange(val) {
      this.currentRow = val
      console.log(val)
    },
    addStock() {
      let formData = new FormData()
      formData.append("productIdArray", this.productIdArray)
      formData.append("number", this.number)
      formData.append("userId", this.userId)
      formData.append("supplierId", this.supplierId)
      this.$axios.post('drp/stock/addStock', formData).then(
          r => {
            console.log(r.data)
            if (r.data == 'ok') {
              location.href = "/index"
            }
          }
      )
    },
    addProduct() {
      if (this.supplierId == '') {
        this.$message.warning("请选则一个供应商")
      } else {
        this.addProduct1 = true
        this.addProduct2 = true
        this.$nextTick(() => {
          this.$refs.addProductRef.init(this.supplierId)
        })
      }
    },
    delProduct() {
      if (this.currentRow == '') {
        this.$message.warning("请选择一个商品")
      } else {
        let formData = new FormData()
        formData.append("productId", this.currentRow.productId)
        this.$axios.post('drp/product/delProduct', formData).then(
            r => {
              if (r.data == '删除成功') {
                location.href = '/index'
              } else {
                this.$message.error(r.data)
              }
            }
        )
      }
    },
    open() {

    }
  }
}

</script>

<style scoped>

</style>